package org.nfIt.controller;

import org.apache.ibatis.annotations.Delete;
import org.nfIt.entity.Article;
import org.nfIt.entity.PageBean;
import org.nfIt.entity.Result;
import org.nfIt.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * ClossName:ArticleController
 * Pakage:org.nfIt.controller
 * Description
 *
 *
 * 文章分类
 *
 * @Author L
 * @Create 2024/6/4 15:39
 * @Version 2023.1.3
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     *
     * 添加文章
     * @param article
     * @return
     */
    @PostMapping
    public Result add(@RequestBody @Validated Article article) {
        articleService.add(article);
        return Result.success();
    }

    /**
     *分页查询
     * PageBean<分页类型> 封装好的分页工具
     *#  @RequestParam(required = false) ：加这个注解 可传递 或 不传递值
     * @param pageNum 当前页码
     * @param pageSize 每页条数
     * @param categoryId 文章分类id
     * @param state 发布状态
     */
    @GetMapping("/list")
    public Result<PageBean<Article>> list
            (
                Integer pageNum,
                Integer pageSize,
                @RequestParam(required = false) Integer categoryId,
                @RequestParam(required = false)String state
            ) {
       PageBean<Article> pageBean = articleService.list(pageNum,pageSize,categoryId,state);
       return Result.success(pageBean);
    }

    /**
     * 修改文章
     * @param article 实体对象
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Article.update.class) Article article) {
        articleService.update(article);
        return Result.success();
    }

    /**
     * 删除文章
     * @param id 文章id
     */
    @DeleteMapping
    public Result delete(Integer id) {
        articleService.delete(id);
        return Result.success();
    }


}
